package com.dxg.msns.music.service.impl;

import com.dxg.msns.music.mapper.PlaylistGradeMapper;
import com.dxg.msns.music.pojo.PlaylistGrade;
import com.dxg.msns.music.service.PlaylistGradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class PlaylistGradeServiceImpl implements PlaylistGradeService {

    @Autowired
    private PlaylistGradeMapper playlistGradeMapper;

    /**
     * 用户对歌单评分，1~5分
     */
    @Override
    public String ratePlaylist(String playlistId, String userId, Integer grade) {
        // 检查是否已经评分
        PlaylistGrade existing = playlistGradeMapper.findByPlaylistIdAndUserId(playlistId, userId);
        if (existing != null) {
            return "您已对该歌单评分，请勿重复评分。";
        }

        // 构建评分对象
        PlaylistGrade playlistGrade = new PlaylistGrade();
        playlistGrade.setPlaylistId(playlistId);
        playlistGrade.setUserId(userId);
        playlistGrade.setPlaylistGrade(grade);
        playlistGrade.setCreateTime(new Date());
        playlistGrade.setUpdateTime(new Date());
        playlistGrade.setStatus(0); // 正常
        System.out.println(playlistGrade);

        // 插入数据库
        playlistGradeMapper.insert(playlistGrade);
        return "评分成功！感谢您的反馈。";
    }
}
